package com.yangcangclub.trade.domain;

import com.google.common.collect.Maps;
import com.yangcangclub.commons.annotation.User;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.trade.base.TradeBaseDomain;
import com.yangcangclub.trade.bean.TradeAddress;
import com.yangcangclub.trade.bean.UserAddress;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by user on 2017/2/27.
 */
@Component
public class UserAddressDomain extends TradeBaseDomain {

    /** 插入一条地址
     * @param userAddress
     * @return
     */
    public int insertUserAddress(UserAddress userAddress){
        if(userAddress.getIsDefault()==1){//如果新数据被设置为默认
            //先将该用户的所有地址设置为非默认
            updateUserAddressUnDefaultByUserId(userAddress.getUserId());
        }
        super.getMasterJdbc().insert("trade.UserAddressMapper.insertUserAddress",userAddress);
        int id = userAddress.getId();
        if(id == 0){
            //回滚
            throw new ApolloException(MessageEnum.TRADE_ADDRESS_ADD_FAILURE);
        }
        return id;
    }

    /**根据地址id更新订单信息
     * @param userAddress
     * @return
     */
    public boolean updateUserAddressById(UserAddress userAddress){
        if(userAddress.getIsDefault()==1){//如果新数据被设置为默认
            //先将该用户的所有地址设置为非默认
            updateUserAddressUnDefaultByUserId(userAddress.getUserId());
        }
        int num = super.getMasterJdbc().update("trade.UserAddressMapper.updateUserAddress",userAddress);
        if(num == 0){
            //回滚
            throw new ApolloException(MessageEnum.USER_ADDRESS_UPDATE_FAILURE);
        }
        return true;
    }

    /**根据用户id查询有效地址（未被删除）
     * @param userId
     * @return
     */
    public List<UserAddress> selectValidUserAddressByUserId(int userId){
        List<UserAddress> userAddressList = super.getSlaveJdbc().selectList("trade.UserAddressMapper.selectValidUserAddressByUserId",userId);
        return userAddressList;
    }

    /**根据id查询地址信息
     * @param id
     * @return
     */
    public UserAddress selectUserAddressById(int id){
        return super.getSlaveJdbc().selectOne("trade.UserAddressMapper.selectUserAddressById",id);
    }

    /**根据用户id更新订单状态（批量，设置为非默认时使用）
     * @param userId
     * @return
     */
    public boolean updateUserAddressUnDefaultByUserId(int userId){
        super.getMasterJdbc().update("trade.UserAddressMapper.updateUserAddressUnDefaultByUserId",userId);
        return true;
    }

    /** 根据地址id更新地址默认状态（单独设置某个地址为默认时使用）
     * @param userId
     * @param id
     * @return
     */
    public boolean updateUserAddressDefaultById(int userId, int id){
        updateUserAddressUnDefaultByUserId(userId);
        int num = super.getMasterJdbc().update("trade.UserAddressMapper.updateUserAddressDefaultById",id);
        if(num == 0){
            //回滚
            throw new ApolloException(MessageEnum.USER_ADDRESS_UPDATE_FAILURE);
        }
        return true;
    }

    /**逻辑删除订单
     * @param id
     * @return
     */
    public boolean deleteUserAddressById(int id){
        int num = super.getMasterJdbc().update("trade.UserAddressMapper.deleteUserAddressById",id);
        if(num == 0){
            //回滚
            throw new ApolloException(MessageEnum.USER_ADDRESS_UPDATE_FAILURE);
        }
        return true;
    }

    /**获取用户默认地址信息
     * @param userId
     * @return
     */
    public UserAddress getDefaultUserAddress(int userId){
        return super.getSlaveJdbc().selectOne("trade.UserAddressMapper.selectDefaultUserAddress",userId);
    }

    public int selectValidUserAddressByUserIdCount(int userId){
        return super.getSlaveJdbc().selectOne("trade.UserAddressMapper.selectValidUserAddressByUserIdCount",userId);
    }
}
